 import http from "http";
 import url from "url";
 import qs from "querystring"
 import path from "path"
 import fs from "fs"


 const server = http.createServer((request, response) => {
     console.log("服务器接收到了请求");
     //  console.log(request.url);//请求
     //  let str = request.url
     // let path = str.slice(0,str.indexOf("?"))
     // let end = str.slice(str.indexOf("?")+1).split("&")
     // let obj = {}
     // end.forEach(item=>{
     //     obj[item.slice(0,item.indexOf("="))] = item.slice(item.indexOf("=")+1)
     // })

     // console.log(obj);

     let ourl = url.parse(request.url)
     console.log(ourl);
     let {
         pathname,
         query
     } = ourl
     console.log(pathname);
     let obj = qs.parse(query)
     console.log(obj);
     //转为绝对路径
     let filePath = path.join(__dirname, pathname)
     //判断数据是否存在

     if (fs.existsSync(filePath)) {
         //判断是否是文件
         if (fs.statSync(filePath).isFile()) {
             let html = fs.readFileSync(filePath)
             console.log(html);
             response.end(html)
             return
         }
         return;
     }

     response.end("404")

 })
 server.listen(5000, () => {
     console.log("服务器启动了");
     console.log("server had run at http://localhost:5000");
 })